<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use think\Db;

class Node extends BasicAdmin
{
    /**
     * 指定当前默认模型
     * @var string
     */
    public $table = 'SystemNode';

    /**
     * 显示节点列表
     */
    public function index()
    {
        return view('');
    }


    public function data()
    {
        $get_datas = $this->request->get();
        $nodes = ToolsService::arr2table(NodeService::get(), 'node', 'pnode');
        $new = [];
        foreach ($nodes as $k => $v){
            if(substr_count($v['node'],'/')==2){
                $new[] = $v;
            }
        }
        $page = $get_datas['page'];
        $limit = $get_datas['limit'];
        $show_data =  array_slice($new,($page-1)*$limit,$limit);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => count($new),
            'data'  => $show_data
        ];
        return json($data);
    }

    /**
     * 保存节点变更
     */
    public function save()
    {
        if ($this->request->isPost()) {
            list($data, $post) = [[], $this->request->post()];
            if (isset($post['list'])) {
                foreach ($post['list'] as $vo) {
                    $data['node'] = $vo['node'];
                    $data[$vo['name']] = $vo['value'];
                }
                !empty($data) && DataService::save($this->table, $data, 'node');
                return json(['code'=>200,'msg'=>'参数保存成功']);
            }
        } else {
            return json(['code'=>0,'msg'=>'参数保存失败']);
        }
    }

    /**
     * 保存节点变更
     */
    public function saveNode()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            !empty($data) && DataService::save($this->table, $data, 'node');
            $nodes = Db::name('SystemNode')->where(['is_auth' => '1'])->column('node');
            cache('need_access_node', $nodes);
            return json(['code'=>200,'msg'=>'参数保存成功']);
        } else {
            return json(['code'=>0,'msg'=>'参数保存失败']);
        }
    }



}
